package com.gl.util.xml;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class StrToXML {
	
	/**
     * 将Transition List转换成XML字符串
     * @param transitionLists
     * @return
     */
    public static String transitionListToXML(List<Model> list) {
       
        String transitions = null;

        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        Element root = document.addElement("config");

        for (Model  model : list) {
            Element itemElement = root.addElement("item");
            itemElement.addAttribute("name", model.getAttr());
            itemElement.addAttribute("id", String.valueOf(model.getI()));
            itemElement.addAttribute("type", String.valueOf(model.isFlag()));
            //itemElement.setText(model.getAttr());
        }

        System.out.println(document);
        
        transitions = XmlSupport.getPrettyXmlString(document);

        return transitions;
    }

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Model> list = new ArrayList<Model>();
		Model m1 = new Model("111", 1, true);
		Model m2 = new Model("222", 2, false);
		Model m3 = new Model("333", 3, true);
		list.add(m1);
		list.add(m2);
		list.add(m3);
		
		System.out.println(transitionListToXML(list));
	}

}
